<template>
  <div>
    <el-dialog
      :visible="showDialog"
      v-bind="$attrs"
      :title="title"
      v-on="$listeners"
      @open="onOpen"
      @close="onClose"
    >
      <el-form
        ref="deptForm"
        :model="formData"
        :rules="rules"
        size="medium"
        label-width="120px"
      >
        <el-form-item label="部门名称" prop="name">
          <el-input
            v-model="formData.name"
            placeholder="请输入部门名称"
            clearable
            :style="{ width: '100%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="部门编号" prop="code">
          <el-input
            v-model="formData.code"
            placeholder="请输入部门编号"
            clearable
            :style="{ width: '100%' }"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select
            v-model="formData.manager"
            placeholder="请选择部门负责人"
            clearable
            :style="{ width: '100%' }"
            @focus="getEmployeeSimple"
          >
            <el-option
              v-for="(item, index) in people"
              :key="index"
              :label="item.username"
              :value="item.username"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input
            v-model="formData.introduce"
            type="textarea"
            placeholder="请输入部门介绍"
            :autosize="{ minRows: 4, maxRows: 4 }"
            :style="{ width: '100%' }"
          ></el-input>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import { addDepartment, getDepartDetail, getDepartments, updateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  components: {},
  inheritAttrs: false,
  props: {
    showDialog: {
      type: Boolean,
      require: true
    },
    treeNode: { // 父部门
      type: Object,
      required: true
    }
  },
  data() {
    const checkName = async(rule, value, callback) => {
      // 兄弟部门中禁止出现重复部门
      // 方式一 ---- 不发请求 得到兄弟
      // const borthor = this.treeNode.children
      // 方式二 --- 发请求
      const { depts } = await getDepartments()
      let borthor
      if (this.formData.id) {
        // 编辑
        borthor = depts.filter(v => v.pid === this.treeNode.id && v.id !== this.treeNode.id) // 排除自己,得到兄弟
      } else {
        // 新增
        borthor = depts.filter(v => v.pid === this.treeNode.id)
      }
      const isExist = borthor.find(v => v.name === value)
      // console.log(isExist)
      isExist ? callback('存在同名的兄弟部门') : callback()
    }
    const checkCode = async(rule, value, callback) => {
      // 部门编码在整个模块中都不允许重复
      const { depts } = await getDepartments() // 全部部门
      let isExist
      if (this.formData.id) {
        // 编辑
        isExist = depts.filter(v => v.id !== this.treeNode.id).some(t => t.code === value)
      } else {
        // 新增
        isExist = depts.some(t => t.code === value)
      }
      isExist ? callback('存在同编号部门') : callback()
    }
    return {
      formData: {
        name: undefined,
        code: undefined,
        manager: undefined,
        introduce: undefined
      },
      rules: {
        name: [{
          required: true,
          message: '请输入部门名称',
          trigger: 'blur'
        }, {
          min: 1,
          max: 50,
          message: '请输入1-50个字符',
          trigger: 'blur'
        }, {
          validator: checkName,
          trigger: 'blur'
        }],
        code: [{
          required: true,
          message: '请输入部门编号',
          trigger: 'blur'
        }, {
          min: 1,
          max: 50,
          message: '请输入1-50个字符',
          trigger: 'blur'
        }, {
          validator: checkCode,
          trigger: 'blur'
        }],
        manager: [{
          required: true,
          message: '请选择部门负责人',
          trigger: 'change'
        }],
        introduce: [{
          required: true,
          message: '请输入部门介绍',
          trigger: 'blur'
        }, {
          min: 1,
          max: 300,
          message: '请输入1-300个字符',
          trigger: 'blur'
        }]
      },
      managerOptions: [{
        'label': '选项一',
        'value': 1
      }, {
        'label': '选项二',
        'value': 2
      }],
      people: []
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑部门' : '新增部门'
    }
  },
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async getDepartDetail(id) {
      const formData = await getDepartDetail(id)
      this.formData = formData
    },
    async getEmployeeSimple() {
      this.people = await getEmployeeSimple()
    },
    onOpen() {},
    onClose() {
      this.close()
    },
    close() {
      // 重置数据
      // this.formData={
      //   name: undefined,
      //   code: undefined,
      //   manager: undefined,
      //   introduce: undefined
      // }
      // 方式一 通过代码手动重置 重置数据和校验
      // this.formData = this.$options.data().formData
      // this.$refs['deptForm'].resetFields()
      this.$emit('update:showDialog', false)
    },
    handelConfirm() {
      // 表单校验
      this.$refs['deptForm'].validate(async valid => {
        if (!valid) return
        // 发请求
        // 判断是编辑还是新增
        if (this.formData.id) {
          // 编辑
          await updateDepartments(this.formData)
        } else {
          // 新增
          await addDepartment({ ...this.formData, pid: this.treeNode.id })
        }
        // 通知父组件做刷新
        this.$emit('addDept')
        // 关闭对话框
        this.close()
      })
    }
  }
}

</script>
<style>
</style>
